package com.hema.auction.activity;

import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hema.auction.R;
import com.hema.auction.base.BaseActivity;
import com.hema.auction.bean.SysUnits;
import com.hema.auction.http.HttpTag;
import com.hema.auction.http.biz.HttpManager;
import com.hema.auction.utils.Constant;
import com.hema.auction.utils.GlideUtils;
import com.hema.auction.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * author by john
 * 启动页
 */
public class SplashActivity extends BaseActivity {

    @BindView(R.id.iv_img)
    ImageView ivImg;
    private long delay = 3000;
    private List<SysUnits> guides;
    private String suspensionAd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        if (!Utils.isEmpty(spManager.getSplashImg())) {
            GlideUtils.load(this, spManager.getSplashImg(), ivImg);
        } else {
            GlideUtils.load(this, R.drawable.ic_splash, ivImg);

        }
        HttpManager.getInstance(this).getSysUnits(this);
        HttpManager.getInstance(this).getNewCount(this);
        postDelayed();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }

    }

    private void postDelayed() {
        getDefaultHandler().postDelayed(() -> {
            if (!isFinishing()) {
                if (!spManager.hasShowGuide()) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Constant.EXTRA_GUIDE, (Serializable) guides);
                    intentTo(GuideActivity.class, bundle);
                } else {
                    intentTo(MainActivity.class);
                }
            }
            finish();
        }, delay);
    }


    //往SD卡写入文件的方法
    public void savaFileToSD(String filename, byte[] bytes) throws Exception {
        //如果手机已插入sd卡,且app具有读写sd卡的权限
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String filePath = Environment.getExternalStorageDirectory().getCanonicalPath() + "/splase";
            File dir1 = new File(filePath);
            if (!dir1.exists()) {
                dir1.mkdirs();
            }
            filename = filePath + "/" + filename;
            //这里就不要用openFileOutput了,那个是往手机内存中写数据的
            FileOutputStream output = new FileOutputStream(filename);
            output.write(bytes);
            //将bytes写入到输出流中
            output.close();
            //关闭输出流
            Toast.makeText(this, "图片已成功保存到" + filePath, Toast.LENGTH_SHORT).show();
        } else Toast.makeText(this, "SD卡不存在或者不可读写", Toast.LENGTH_SHORT).show();
    }


    @Override
    protected void onSuccess(HttpTag httpTag, Call call, JSONObject result) {
        super.onSuccess(httpTag, call, result);
        switch (httpTag) {
            case TAG_GET_SYS_UNITS:
                try {
                    if (result.getInt("errcode") == 0) {
                        List<SysUnits> sysUnitses = new Gson().fromJson(result.getJSONArray("data").toString(), new TypeToken<List<SysUnits>>() {
                        }.getType());
                        if (sysUnitses != null) {
                            for (SysUnits sysUnits : sysUnitses) {
                                if (sysUnits.getTab().equals("banner_back")) {
                                    spManager.setSplashImg(sysUnits.getValue());
                                    continue;
                                }
                                if (sysUnits.getTab().equals("banner_home")) {
                                    aCache.put(Constant.CACHE_INDEX_BANNER, sysUnits);
                                    continue;
                                }
                                if (sysUnits.getTab().equals("ten_area")) {
                                    aCache.put(Constant.CACHE_TEN_AREA, sysUnits);
                                    continue;
                                }
                                if (sysUnits.getTab().equals("android_version")) {
                                    spManager.setHttpVersionCode(Integer.valueOf(sysUnits.getValue()));
                                    continue;
                                }
                                if (sysUnits.getTab().equals("apk_url")) {
                                    spManager.setHttpApkUrl(sysUnits.getValue());
                                    continue;
                                }
                                if (sysUnits.getTab().equals("new_guide")) {
                                    HttpManager.getInstance(this).getBanner(sysUnits.getValue(), this);
                                    continue;
                                }
                                if (sysUnits.getTab().equals("Suspension_ad")) {
                                    suspensionAd = sysUnits.getValue();
                                    continue;
                                }
                                if (sysUnits.getTab().equals("is_back_open")) {
                                    spManager.setShowNoBaopei(sysUnits.getValue().equals("1"));
                                    continue;
                                }
                                if (sysUnits.getTab().equals("new_guide_pic")) {
                                    aCache.put(Constant.CACHE_NEW_GUIDE_PIC, sysUnits);
                                    continue;
                                }
                            }
                            spManager.setSuspensionAd(suspensionAd);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case TAG_GET_NEW_COUNT:
                try {
                    if (result.getInt("errcode") == 0) {
//                        spManager.setNewMessageCount(result.getJSONObject("data").getInt("msg_count"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case TAG_GET_BANNER:
                try {
                    if (result.getInt("errcode") == 0) {
                        guides = new Gson().fromJson(result.getJSONArray("data").toString(), new TypeToken<List<SysUnits>>() {
                        }.getType());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

}
